/*
 * eeprom.h
 *
 *  Created on: 2010-12-23
 *      Author: minfang
 */

#ifndef EEPROM_H_
#define EEPROM_H_

#define EE_ADDRESS_LOW_TEMP_START 0
#define EE_ADDRESS_LOW_TEMP_STOP 1
#define EE_ADDRESS_HIGH_TEMP_START 2
#define EE_ADDRESS_HIGH_TEMP_STOP 3
#define EE_ADDRESS_LIGHT_DAY 4
#define EE_ADDRESS_LIGHT_NIGHT 5
#define EE_NO_WARN_CAP_TIME 6

volatile char temp_alarm_low_start =20;
volatile char temp_alarm_low_stop=21;

volatile char temp_alarm_high_start=30;
volatile char temp_alarm_high_stop=29;

volatile char light_night=89;
volatile char light_day=75;

volatile char no_warn_cap_time=1;

void write_eeprom(unsigned int address,  char value) {
	while (EECR & (1 << EEWE))
		;

	EEAR = address;

	EEDR = value;

	EECR |= (1 << EEMWE);

	EECR |= (1 << EEWE);
}

 char read_eeprom(unsigned int address) {
	while (EECR & (1 << EEWE))
		;

	EEAR = address;



	EECR |= (1 << EERE);

	return EEDR;
}

void save_all(){


	write_eeprom(EE_ADDRESS_LOW_TEMP_START,temp_alarm_low_start);
	write_eeprom(EE_ADDRESS_LOW_TEMP_STOP,temp_alarm_low_stop);
	write_eeprom(EE_ADDRESS_HIGH_TEMP_START,temp_alarm_high_start);
	write_eeprom(EE_ADDRESS_HIGH_TEMP_STOP,temp_alarm_high_stop);
	write_eeprom(EE_ADDRESS_LIGHT_NIGHT,light_night);
	write_eeprom(EE_ADDRESS_LIGHT_DAY,light_day);
	write_eeprom(EE_NO_WARN_CAP_TIME,no_warn_cap_time);


}
void load_all(){

	temp_alarm_low_start=read_eeprom(EE_ADDRESS_LOW_TEMP_START);
	temp_alarm_low_stop=read_eeprom(EE_ADDRESS_LOW_TEMP_STOP);
	temp_alarm_high_start=read_eeprom(EE_ADDRESS_HIGH_TEMP_START);
	temp_alarm_high_stop=read_eeprom(EE_ADDRESS_HIGH_TEMP_STOP);
	light_night=read_eeprom(EE_ADDRESS_LIGHT_NIGHT);
	light_day=read_eeprom(EE_ADDRESS_LIGHT_DAY);
	no_warn_cap_time=read_eeprom(EE_NO_WARN_CAP_TIME);
}
#endif /* EEPROM_H_ */
